package com.sakura.mall.ware.wareSku.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.ware.wareSku.domain.entity.WareSkuEntity;
import com.sakura.mall.ware.wareSku.domain.request.AddWareSkuRequest;
import com.sakura.mall.ware.wareSku.domain.request.UpdateWareSkuRequest;
import com.sakura.mall.ware.wareSku.domain.request.WareSkuPageRequest;
import com.sakura.mall.ware.wareSku.domain.vo.WareSkuVo;
import com.sakura.mall.ware.wareSku.mapper.WareSkuMapper;
import com.sakura.mall.ware.wareSku.service.WareSkuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品库存 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class WareSkuServiceImpl extends ServiceImpl<WareSkuMapper, WareSkuEntity> implements WareSkuService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddWareSkuRequest request) {
        // todo 其他校验逻辑
        WareSkuEntity wareSku = request.convert(WareSkuEntity.class);
        this.save(wareSku);
    }

    @Override
    public Page<WareSkuVo> queryPage(WareSkuPageRequest request) {
        IPage<WareSkuEntity> qp = request.toPage();
        LambdaQueryWrapper<WareSkuEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<WareSkuVo> ret = qp.convert(entity -> entity.convert(WareSkuVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateWareSkuRequest request) {
        WareSkuEntity entity = request.convert(WareSkuEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

